Env.aggregate(window.String.prototype, {
	
	/**
	 * Returns capitalized new String.
	 */
	capitalize: function() {
		return this[0].toUpperCase() + this.substr(1);
	},
	
	// TODO for removal - 10x slower than running str.indexOf(substr) from the code.
	// Firefox & Chrome probably optimizes indexOf running on the explicit string?
	/**
	 * Returns true if the String contains the given regular expression.
	 */
	contains: function(substr) {
		"use strict";
		return this.indexOf(substr) !== -1;
	},
	
	/**
	 * Returns true if the string doesn't contain any characters.
	 */
	empty: function() {
		"use strict";
		return this.length === 0;
	},
	
	/**
	 * Returns new String padded with spaces to the left, matching the specified length
	 */
	lpad: function(length) {
		"use strict";
		var str = '' + this;
	    while (str.length < length) { str = ' ' + str; }
	    return str;
	},
	
	/**
	 * "Pops" last tokenized substring from the String.
	 * @param String token - to split into array (default ".")
	 */
	pop: function(token) {
		"use strict";
		var arr = this.split(token || ".");
		return arr[arr.length - 1];
	},
	
	/**
	 * Returns new String padded with spaces to the right, matching the specified length
	 */
	rpad: function(length) {
		"use strict";
		var str = '' + this;
	    while (str.length < length) { str += ' '; }
	    return str;
	},
	
	/**
	 * "Shifts" first tokenized substring from the String.
	 * @param String token - to split into array
	 */
	shift: function(token) {
		"use strict";
		return this.split(token|| ".")[0];
	},
	
	/**
	 * Returns true if the String starts with specified prefix.
	 */
	startsWith: function(prefix) {
		"use strict";
		return this.search(new RegExp("^" + prefix)) !== -1;
	},
	
	/**
	 * Trims the string of any spaces before or after.
	 */
	trim: function() {
		"use strict";
		var str = this.replace(/^\s\s*/, ''),
			i = str.length, ws = /\s/;

		while(ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	}
});

Env.String = {
	trim: function(str) {
		var str = str.replace(/^\s\s*/, ''),
			i = str.length, ws = /\s/;

		while(ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	}
}